package com.neusoft.chatserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.neusoft.chatserver.dto.GroupCreate;
import com.neusoft.chatserver.dto.GroupModify;
import com.neusoft.chatserver.dto.RespBean;
import com.neusoft.chatserver.dto.UserQuery;
import com.neusoft.chatserver.entity.ChatGroup;
import com.neusoft.chatserver.entity.UserGroup;
import com.neusoft.chatserver.entity.Users;
import com.neusoft.chatserver.mapper.ChatGroupMapper;
import com.neusoft.chatserver.mapper.UserGroupMapper;
import com.neusoft.chatserver.mapper.UsersMapper;
import com.neusoft.chatserver.service.IChatGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuxp
 * @since 2021-07-15
 */
@Service
public class ChatGroupServiceImpl extends ServiceImpl<ChatGroupMapper, ChatGroup> implements IChatGroupService {

    @Autowired
    ChatGroupMapper chatGroupMapper;

    @Autowired
    UserGroupMapper userGroupMapper;

    @Autowired
    UsersMapper usersMapper;

    @Override
    public RespBean createGroup(GroupCreate groupCreate) {
        RespBean respBean = RespBean.ok(200 , "Group created successfully!");
        ChatGroup group = new ChatGroup();
        group.setGroupName(groupCreate.getGroupName());
        chatGroupMapper.insert(group);
        Integer groupId = group.getGroupId();
        for(Integer userId : groupCreate.getUserIds()){
            UserGroup userGroup = new UserGroup();
            userGroup.setGroupId(groupId);
            userGroup.setUserId(userId);
            userGroupMapper.insert(userGroup);
        }
        return respBean;
    }

    @Override
    public RespBean addGroupMember(GroupModify groupModify) {
        RespBean respBean = RespBean.ok(200, "Group member added successfully!");
        UserGroup userGroup = new UserGroup();
        userGroup.setGroupId(groupModify.getGroupId());
        userGroup.setUserId(groupModify.getUserId());
        userGroupMapper.insert(userGroup);
        return respBean;
    }
    @Override
    public RespBean deleteGroupMember(GroupModify groupModify) {
        RespBean respBean = RespBean.ok(200, "Group member deleted successfully!");
        LambdaQueryWrapper<UserGroup> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserGroup::getGroupId, groupModify.getGroupId()).eq(UserGroup::getUserId, groupModify.getUserId());
        userGroupMapper.delete(wrapper);
        return respBean;
    }

    @Override
    public RespBean queryGroupMember(Integer groupId) {
        RespBean respBean = RespBean.ok(200, "Find member successfully!");
        LambdaQueryWrapper<UserGroup> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserGroup::getGroupId, groupId);
        List<UserGroup> userGroups = userGroupMapper.selectList(wrapper);
        List<UserQuery> members = new ArrayList<>();
        for(UserGroup userGroup : userGroups){
            Integer userId = userGroup.getUserId();
            UserQuery userQuery = new UserQuery(usersMapper.selectById(userId));
            members.add(userQuery);
        }
        respBean.setObj(members);
        return respBean;
    }

    @Override
    public RespBean getAllGroup(Integer userId) {
        RespBean respBean = RespBean.ok(200, "Find groups successfully!");
        LambdaQueryWrapper<UserGroup> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserGroup::getUserId, userId);
        List<UserGroup> userGroups = userGroupMapper.selectList(wrapper);
        Set<Integer> set = new HashSet<>();
        for(UserGroup userGroup : userGroups){
            set.add(userGroup.getGroupId());
        }
        List<ChatGroup> groups = chatGroupMapper.selectBatchIds(set);
        respBean.setObj(groups);
        return respBean;
    }

    @Override
    public RespBean getGroupMember(Integer groupId) {
        RespBean respBean = RespBean.ok(200, "Find group members successfully.");
        LambdaQueryWrapper<UserGroup> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserGroup::getGroupId, groupId);
        List<UserGroup> userGroups = userGroupMapper.selectList(wrapper);
        List<UserQuery> members = new ArrayList<>();
        for(UserGroup userGroup : userGroups){
            Users user = usersMapper.selectById(userGroup.getUserId());
            members.add(new UserQuery(user));
        }
        respBean.setObj(members);
        return respBean;
    }


}
